package com.redfootdev.horseskills;
import org.bukkit.ChatColor;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;

import com.redfootdev.horseskills.Skill;
import com.redfootdev.horseskills.Database;
import com.redfootdev.horseskills.HorseDatabase;
import com.redfootdev.horseskills.HorseMonitor;

public class Trample{
	
	
	public Trample(){
		
	}
	//
	// horseHitsomething() - This is the top level routine that is called
	// 						by the event handler for a horse colliding in to something.
	// 						This routine expects that the caller made sure that it was 
	//						a ridden horse that hit something. If this routine is called, 
	//						we should have already noticed the horse is being ridden and 
	//						it should have been already added to the horse database. 
	//						We also assume the player is in the player database.
	//
	public int horseTrampledSomething(HorseDatabase hdb, PlayerDatabase pdb, Entity horse, Player rider, Entity victim) {
		// first decide if there is going to be any damage.
		boolean canRunOver;
		EntityType type = victim.getType();
		canRunOver = type == EntityType.ZOMBIE ||
					 type == EntityType.CHICKEN ||
					 type == EntityType.CREEPER ||
					 type == EntityType.ENDERMAN ||
					 type == EntityType.OCELOT ||
					 type == EntityType.ZOMBIE ||
					 type == EntityType.WOLF ||
					 type == EntityType.WITHER ||
					 type == EntityType.PIG ||
					 type == EntityType.PIG_ZOMBIE ||
					 type == EntityType.SPIDER ||
					 type == EntityType.VILLAGER ||
					 type == EntityType.SNOWMAN ||
					 type == EntityType.SLIME ||
					 type == EntityType.SKELETON ||
					 type == EntityType.SILVERFISH ||
					 type == EntityType.PLAYER;
		
		if(!canRunOver){
			return 0;
		}
		
		// look up the horse and player attributes.
		Skill trample = hdb.getTrample(horse);
		Skill handling = hdb.getHandling(horse, rider);
		Skill horsemanship = pdb.getHorsemanship(rider);
		
		// determine if entity was "missed"
		if (Math.random() > trampleChance(trample, handling)){
			return 0;
		}
		
		// calculate standard damage.
		double damage = trample.getLevel()/25.0 + 1;
	
		((LivingEntity) victim).setLastDamage(damage);
		// calculate the XP gain for trample.
		int xpGainTrample = (int)(damage*20*(1+horsemanship.getLevel()/100));
		
		// apply damage to victim
		// TODO: need to set last damage cause.
		((LivingEntity)victim).damage(damage);
		
		// apply XP to trample Skill
		if(trample.addXP(xpGainTrample)) {
			rider.sendMessage(ChatColor.AQUA + "Your Trample level is now " + trample.getLevel() + ".");
		}
		hdb.setTrample(horse, trample);
		
		return xpGainTrample;
		
		// TODO: update any others attributes
		
	}
	
	
	public String toString(HorseDatabase hdb, PlayerDatabase pdb, Entity horse, Player rider) {

		Skill trample = hdb.getTrample(horse);


		int level = trample.getLevel();
		int xp = trample.getXP();

		
		String stats = "Trample Level: " + level + "\nExperience: " + xp;
		return stats;
	}
	public double trampleChance(Skill trample, Skill handling) {
		return (0.5 + (0.35*(trample.getLevel()/100)) + (0.1*(handling.getLevel())/100));
	}
		

}
